/*
 * @Author: 曹捷
 * @Date: 2020-08-10 11:12:29
 * @LastEditors: 曹捷
 * @LastEditTime: 2020-08-24 10:14:29
 * @Description: 系统共用设置
 */
import http from '@/config'

/**
 * 检测系统版本
 * @param {*} callback 
 */
export function appUpdate(callback) {
    // #ifdef APP-PLUS
    http.getAppUpdate().then(res => {
        console.log('appUpdate -> res', res)
        console.log('appUpdate -> plus.runtime.version', plus.runtime.version)
        let appVersion = res.app_version.replace(/\./g, '')
        let sysVersion = plus.runtime.version.replace(/\./g, '')
        console.log('appUpdate -> appVersion', appVersion)
        if (appVersion > sysVersion) {
            if (res.app_update === '1') {
                // 整包升级 暂时跳转外链
                uni.showModal({
                    title: '提示',
                    content: '请更新新版本',
                    showCancel: false,
                    success: function (resModal) {
                        if (resModal.confirm) {
                            plus.runtime.openURL(res.app_url);
                        }
                    }
                });
                uni.showLoading({
                    title: '等待更新中...',
                    mask: true
                });
            } else if (res.app_update === '2' && res.app_wgt_url) {
                // 热更新 
                uni.downloadFile({
                    url: res.app_wgt_url,
                    success: (downloadResult) => {
                        if (downloadResult.statusCode === 200) {
                            plus.runtime.install(downloadResult.tempFilePath, {
                                force: false
                            }, function () {
                                console.log('install success...');
                                plus.runtime.restart();
                            }, function (e) {
                                console.error('install fail...');
                            });
                        }
                    }
                });
            }
        } else {
            callback && callback()
        }
    })
    // #endif
    // #ifndef APP-PLUS
    uni.showToast({
        icon: 'none',
        title: '当前已经是最新版本啦',
    })
    // #endif

}